package com.baizhi.service.imp;

import com.baizhi.dao.RoleDao;
import com.baizhi.entity.Node;
import com.baizhi.entity.Role;
import com.baizhi.service.RoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class RoleServiceImp implements RoleService {

    @Resource
    private RoleDao roleDao;

    @Override
    public Set<Role> findRolesByAdminAdminName(String adminName) {
        return roleDao.selectRolesByAdminName(adminName);
    }

    @Override
    public List<Role> findAll() {
        return roleDao.selectAll();
    }

    @Override
    public List<Map> findSubIds(Integer parentId) {
        return roleDao.selectSubIds(parentId);
    }

    @Override
    public List<Node> findSubNodes(Integer id) {
        return roleDao.selectSubNodes(id);
    }

    @Override
    public List<Map> findPermissionByRoleName(List<String> roles) {
        return roleDao.selectPermissionByRoleName(roles);
    }

    @Override
    public Role findRoleByName(String roleName) {
        return roleDao.selectRoleByName(roleName);
    }

    @Override
    public void addRole(Role role) {
        roleDao.insertRole(role);
    }

    @Override
    public void removeRoleByName(String roleName) {
        roleDao.deleteRoleByName(roleName);
    }

    @Override
    public void renovateRole(Role role) {
        roleDao.updateRole(role);
    }

    @Override
    public List<Map> findPermissionByAdminName(String adminName) {
        Set<Role> roles = roleDao.selectRolesByAdminName(adminName);
        ArrayList list = new ArrayList();
        roles.stream().forEach((role)->{
            list.add(role.getRoleName());
        });
        return roleDao.selectPermissionByRoleName(new ArrayList<String>(list));
    }
}
